<template>
    <div class="login_container">
        <!-- 登录界面 -->
        <div class="login_box">
            <!-- 头部图片logo -->
            <div class="head_box">
                <img src="../assets/logo.png" alt />
            </div>
            <!-- form表单 使用element-ui-->
            <el-form
                ref="LoginFormRef"
                :model="formData"
                :rules="rules"
                label-width="0px"
                class="formLogin"
            >
                <el-form-item prop="username">
                    <el-input
                        v-model="formData.username"
                        placeholder="请输入用户名"
                        prefix-icon="iconfont icon-user"
                    ></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        v-model="formData.password"
                        type="password"
                        placeholder="请输入密码"
                        prefix-icon="iconfont icon-showpassword"
                    ></el-input>
                </el-form-item>
                <el-form-item class="btn">
                    <el-button type="primary" @click="onSubmit">提交</el-button>
                    <el-button type="info" @click="onCancle">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            formData: {
                username: 'admin',
                password: '123456'
            },
            rules: {
                username: [
                    {
                        required: true,
                        message: '请输入用户名',
                        trigger: 'blur'
                    },
                    {
                        min: 3,
                        max: 12,
                        message: '长度在 3 到 12 个字符',
                        trigger: 'blur'
                    }
                ],
                password: [
                    {
                        required: true,
                        message: '请输入密码',
                        trigger: 'blur'
                    },
                    {
                        min: 6,
                        max: 18,
                        message: '长度在 6 到 18 个字符',
                        trigger: 'blur'
                    }
                ]
            }
        }
    },
    methods: {
        onSubmit() {
            //提交
            this.$refs.LoginFormRef.validate(async (valid) => {
                //如果表单内容有误，跳出
                if (!valid) return
                //向服务器发送请求
                const { data: res } = await this.$http.post(
                    'login',
                    this.formData
                )
                if(res.meta.status!==200) 
                    return this.$message.error('登陆失败: '+res.meta.msg);
                this.$message.success('登录成功')
                //登录成功之后将token值添加到sessionStorage中
                window.sessionStorage.setItem('token',res.data.token)
                //跳转
                this.$router.push('/home')
            })
        },
        onCancle() {
            //重置
            this.$refs.LoginFormRef.resetFields()
            console.log(1)
        }
    }
}
</script>

<style lang='less' scoped>
.login_container {
    height: 100%;
    background-color: #2b5b6b;
}
.login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .head_box {
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        background-color: #fff;
        left: 50%;
        transform: translate(-50%, -50%);
        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
}
.formLogin {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
.btn {
    display: flex;
    justify-content: flex-end;
}
</style>